package com.xintai.sparepart.spareandpart.controller;

import java.util.List;

import com.xintai.common.constant.Constants;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.xintai.common.annotation.Log;
import com.xintai.common.core.controller.BaseController;
import com.xintai.common.core.domain.AjaxResult;
import com.xintai.common.enums.BusinessType;
import com.xintai.sparepart.spareandpart.domain.BeiProducts;
import com.xintai.sparepart.spareandpart.service.IBeiProductsService;
import com.xintai.common.utils.poi.ExcelUtil;
import com.xintai.common.core.page.TableDataInfo;

/**
 * 备品备件货品Controller
 *
 * @author xintai
 * @date 2020-12-23
 */
@RestController
@RequestMapping("/sparepart/products")
public class BeiProductsController extends BaseController
{
    @Autowired
    private IBeiProductsService beiProductsService;

    /**
     * 查询备品备件货品列表
     */
    @PreAuthorize("@ss.hasPermi('sparepart:products:list')")
    @GetMapping("/list")
    public TableDataInfo list(BeiProducts beiProducts)
    {
        startPage();
        beiProducts.setProIsdel(Constants.SUCCESS);
        List<BeiProducts> list = beiProductsService.selectBeiProductsList(beiProducts);
        return getDataTable(list);
    }

    /**
     * 导出备品备件货品列表
     */
    @PreAuthorize("@ss.hasPermi('sparepart:products:export')")
    @Log(title = "备品备件货品", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(BeiProducts beiProducts)
    {
        List<BeiProducts> list = beiProductsService.selectBeiProductsList(beiProducts);
        ExcelUtil<BeiProducts> util = new ExcelUtil<BeiProducts>(BeiProducts.class);
        return util.exportExcel(list, "products");
    }

    /**
     * 获取备品备件货品详细信息
     */
    @PreAuthorize("@ss.hasPermi('sparepart:products:query')")
    @GetMapping(value = "/{proId}")
    public AjaxResult getInfo(@PathVariable("proId") Long proId)
    {
        return AjaxResult.success(beiProductsService.selectBeiProductsById(proId));
    }

    /**
     * 新增备品备件货品
     */
    @PreAuthorize("@ss.hasPermi('sparepart:products:add')")
    @Log(title = "备品备件货品", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BeiProducts beiProducts)
    {
        if (beiProductsService.selectProductsnamesake(beiProducts)){
            return AjaxResult.error("新增失败，'"+beiProducts.getProName()+"'货品已存在");
        }
        return toAjax(beiProductsService.insertBeiProducts(beiProducts));
    }

    /**
     * 修改备品备件货品
     */
    @PreAuthorize("@ss.hasPermi('sparepart:products:edit')")
    @Log(title = "备品备件货品", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BeiProducts beiProducts)
    {
        return toAjax(beiProductsService.updateBeiProducts(beiProducts));
    }

    /**
     * 删除备品备件货品
     */
    @PreAuthorize("@ss.hasPermi('sparepart:products:remove')")
    @Log(title = "备品备件货品", businessType = BusinessType.DELETE)
	@DeleteMapping("/{proIds}")
    public AjaxResult remove(@PathVariable Long[] proIds)
    {
        return toAjax(beiProductsService.deleteBeiProductsByIds(proIds));
    }

    /**
     * 审核
     * @param beiProducts
     * @return
     */
    @PreAuthorize("@ss.hasPermi('sparepart:products:check')")
    @Log(title = "备品备件审核", businessType = BusinessType.OTHER)
    @PutMapping("/check")
     public AjaxResult checkProducts(@RequestBody BeiProducts beiProducts){
         return toAjax(beiProductsService.checkProduct(beiProducts));
     }
}
